//
//  BaseViewController.m
//  CityFire
//
//  Created by Liuyz on 2017/7/30.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "BaseViewController.h"
#import "UINavigationBar+Background.h"
#import "MJRefreshNormalHeader+Extension.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
//    self.extendedLayoutIncludesOpaqueBars=YES;
    
    //如果页面导航栏下边出现黑条，就把上句代码放开。印证了作者这句话：但若translucent属性为NO，则除非设置controller的extendedLayoutIncludesOpaqueBars属性为YES，topViewControll.view都是不包含此区域的。
    
    self.navigationController.navigationBar.translucent=NO;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self setupRefreshView];
    
    // 只有打开了自动刷新才进行自动刷新
    if (self.isAutoRefresh) {
        [self startRefresh];
    }
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [self.refreshScrollView.mj_header endRefreshing];
    [self.refreshScrollView.mj_footer endRefreshing];
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.translucent=YES;
    
}

-(UIStatusBarStyle)preferredStatusBarStyle

{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 默认从第一页开始
    self.pageNum = 0;
    
//    [self bindUserReloadNotification];
}

//- (void)bindUserReloadNotification
//{
//    @weakify(self)
//    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:kLoginSuccessNotifation object:nil]
//      takeUntil:[self rac_willDeallocSignal]]
//     subscribeNext:^(id x) {
//         @strongify(self)
//         DLog(@"用户信息已经重新读取！");
//         if (self.reloadUserInfoActionBlock) {
//             self.reloadUserInfoActionBlock();
//         }
//     }];
//}

- (void)setupRefreshView {
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的startInitRequest方法）
    if (self.refreshScrollView) {
        // 设置header
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerForCustomWithRefreshingTarget:self refreshingAction:@selector(startInitRequest)];
        self.refreshScrollView.mj_header = header;
        
        if (self.isPageSep == YES) {
            MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(startMoreRequest)];
            self.refreshScrollView.mj_footer = footer;
        }
    }
}

- (void)setIsPageSep:(BOOL)isPageSep
{
    if (isPageSep) {
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(startMoreRequest)];
        self.refreshScrollView.mj_footer = footer;
    }else{
        //        if (![self.view.loadingView isLoading]) {
        //            [self.view beginLoading];
        //        }
        self.refreshScrollView.mj_footer = nil;
    }
}

- (void)startInitRequest
{
    //    if (![self.view.loadingView isLoading]) {
    //        [self.view beginLoading];
    //    }
    
    self.pageNum = 0;
    if (self.refreshActionBlock) {
        self.refreshActionBlock();
    }
}

- (void)startMoreRequest
{
    //    if (![self.view.loadingView isLoading]) {
    //        [self.view beginLoading];
    //    }
    
    self.pageNum ++;
    if (self.loadMoreActionBlock) {
        self.loadMoreActionBlock();
    }
}

/**
 *  开始初始化刷新调用
 */
- (void)startRefresh
{
    // 当超过了自动刷新时间，自动刷新
    NSDate *now = [NSDate date];
    NSTimeInterval timeInterval = [now timeIntervalSinceDate:lastAutoRefreshDate];
    if (timeInterval > V_MAX_AUTO_REFRESH_INTEVER || lastAutoRefreshDate == nil) {
        [self.refreshScrollView.mj_header beginRefreshing];
        lastAutoRefreshDate = now;
    }
    
}

- (void)endRefresh
{
//    [self.refreshScrollView endLoading];
    [self.refreshScrollView.mj_header endRefreshing];
    [self.refreshScrollView.mj_footer endRefreshing];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
